package com.ecnu.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectAnnouncement;
import com.ecnu.system.domain.dto.AnnouncementDTO;
import com.ecnu.system.domain.dto.AnnouncementTopDTO;
import com.ecnu.system.domain.dto.ProjectAnnouncementDTO;
import com.ecnu.system.service.IProjectAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "活动内通知公告管理")
@RestController
@RequestMapping("/project/announcement")
@Validated
public class ProjectAnnouncementController extends BaseController {
    @Autowired
    private IProjectAnnouncementService announcementService;

    /******
     *
     前台
     */
    /**
     * 活动公告列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "活动公告列表")
    public TableDataInfo AnnouncementList(@Valid @RequestBody AnnouncementDTO dto) {
        return announcementService.announcementList(dto);
    }

    /**
     * 活动公告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "活动公告详情")
    public AjaxResult info(@RequestParam("id") String id) {
        return announcementService.infoById(id);
    }


    /*****
     * 后台管理
     * @param dto
     * @return
     */

    /**
     * 活动公告编辑
     *
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation(value = "活动公告编辑")
    public AjaxResult edit(@Valid @RequestBody ProjectAnnouncementDTO dto) {

        LoginUser loginUser = getLoginUser();

        return announcementService.edit(dto, loginUser);
    }

    @PostMapping("/editForTop")
    @ApiOperation(value = "活动公告置顶修改")
    public AjaxResult edit(@RequestBody AnnouncementTopDTO dto) {

        LoginUser loginUser = getLoginUser();
        ProjectAnnouncement one = announcementService.getOne(new QueryWrapper<ProjectAnnouncement>()
                .eq("id", dto.getId())
                .eq("del_flag", "0"));
        if (one == null) {
            return AjaxResult.error("活动公告不存在");
        }
        ProjectAnnouncement announcement = new ProjectAnnouncement();
        announcement.setId(dto.getId());
        announcement.setIsTop(dto.getIsTop());
        return AjaxResult.success(announcementService.updateById(announcement));
    }


    /**
     * 删除公告
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除公告")
    @PostMapping("/del")
//    @PreAuthorize("@ss.hasPermi('project:announcement:del')")
    public AjaxResult del(@RequestBody String[] ids) {

        try {
            LoginUser loginUser = getLoginUser();
            List<ProjectAnnouncement> projects = Arrays.stream(ids)
                    .map(id -> {
                        ProjectAnnouncement project = new ProjectAnnouncement();
                        project.setId(id);
                        project.setDelFlag("2");
                        return project;
                    })
                    .collect(Collectors.toList());
            return announcementService.updateBatchById(projects) ?
                    AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
        } catch (Exception e) {
            logger.error("活动删除错误" + e.getMessage());
            return AjaxResult.error("删除失败");
        }
    }
}
